<?php

namespace app\controller\admin;

use app\bll\SiteAdmin as BllSiteAdmin;
use app\bll\SiteRole as BllSiteRole;
use app\bll\User as BllUser;
use core\controller\AdminBase;
use Exception;
use support\View;

class Admin extends AdminBase
{
    public function index()
    {

    
        //获取所有角色
        $bllSiteRole = new BllSiteRole();
        $roles       = $bllSiteRole->select(request()->nowsiteid);
        $roles       = array_column($roles, null, 'id');

        $bllSiteAdmin = new BllSiteAdmin();
        $admins = $bllSiteAdmin->paginator(
            request()->nowsiteid,
            request()->get('username_phone', '')
        );
        View::assign('roles', $roles);
        View::assign('admins', $admins);
        return $this->view();
    }


    /**
     * 添加管理员
     *
     * @return void
     */
    public function add()
    {
        if (is_post()) {
            try {
                $bllSiteAdmin   = new BllSiteAdmin();
                $data           = request()->post();
                $data['siteid'] = request()->nowsiteid;
                $bllSiteAdmin->add($data);
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {

            if (request()->get('type', '') == 'seluser') {
                $bllUser = new BllUser();
                $users = $bllUser->paginator(request()->get('searchkeyword', ''), 10);
                return $this->success([
                    'count' => $users->total(),
                    'item' => $users->items()
                ]);
            }
            //获取本站点下的角色
            $bllSiteRole = new BllSiteRole();
            $roles = $bllSiteRole->select(request()->nowsiteid);
            View::assign('roles', $roles);
            return $this->view();
        }
    }


    /**
     * 编辑
     *
     * @return void
     */
    public function edit()
    {
        $bllSiteAdmin = new BllSiteAdmin();
        $id           = request()->get('id');
        if (is_post()) {
            try {
                $bllSiteAdmin   = new BllSiteAdmin();
                $data           = request()->post();
                $bllSiteAdmin->edit(request()->get('id'), request()->nowsiteid, $data);
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            if (request()->get('type', '') == 'seluser') {
                $bllUser = new BllUser();
                $users = $bllUser->paginator(request()->get('searchkeyword', ''), 10);
                return $this->success([
                    'count' => $users->total(),
                    'item' => $users->items()
                ]);
            }
            //获取本站点下的角色
            $bllSiteRole = new BllSiteRole();
            $roles = $bllSiteRole->select(request()->nowsiteid);

            //查询
            $admininfo = $bllSiteAdmin->find($id, request()->nowsiteid);
            View::assign('admininfo', $admininfo);
            View::assign('roles', $roles);

            return $this->view();
        }
    }

    /**
     * 删除
     *
     * @return void
     */
    public function delete()
    {
        try {
            $bllSiteAdmin = new BllSiteAdmin();
            $bllSiteAdmin->delete(
                request()->get('uid'),
                request()->nowsiteid
            );
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }
}
